/**   
 * @Title: TestLogDaoImplementation.java 
 * @Package cn.edu.cdtu.healthserver.dao.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.SQLToken;
import cn.edu.cdtu.healthserver.dao.TestLogDao;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestLog;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/** 
 * @ClassName: TestLogDaoImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class TestLogDaoImplementation implements TestLogDao {
	
	private static final String TABLE_STRING = " test_log ";
	private static final String ATTRIBUTE_ID_STRING = " id ";
	private static final String ATTRIBUTE_USER_ID_STRING = " user_id ";
	private static final String ATTRIBUTE_TEST_ID_STRING = " test_id ";
	private static final String ATTRIBUTE_DATE_STRING = " date ";
	private static final String ATTRIBUTE_POINT_SUM_STRING = " point_sum ";
	
	/**
	 * 数据库查询器
	 */
	private QueryRunner queryRunner = 
			new QueryRunner(C3P0Util.getDataSource());

	/* (non-Javadoc)
	 * <p>Title: add</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#add(java.lang.Object)
	 */
	@Override
	public boolean add(TestLog object) {
		// TODO Auto-generated method stub
	  	final String sqlString = ""
	  			+ SQLToken.INSERT_INTO_STRING
				+ TABLE_STRING
				+ "("
				+ ATTRIBUTE_USER_ID_STRING + ","
				+ ATTRIBUTE_TEST_ID_STRING + ","
				+ ATTRIBUTE_DATE_STRING + ","
				+ ATTRIBUTE_POINT_SUM_STRING
				+ ")"
				+ SQLToken.VALUES_STRING + "(?,?,?,?)";
		Object[] paramsObject = new Object[]{
				object.getUser_id(),object.getTest_id(),
				object.getDate(),object.getPoint_sum()
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: update</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#update(java.lang.Object)
	 */
	@Override
	public boolean update(TestLog object) {
		// TODO Auto-generated method stub
	  	final String sqlString = ""
	  			+ SQLToken.UPDATE_STRING
	  			+ TABLE_STRING
	  			+ SQLToken.SET_STRING
	  			+ ATTRIBUTE_USER_ID_STRING + "=?,"
	  			+ ATTRIBUTE_TEST_ID_STRING + "=?,"
	  			+ ATTRIBUTE_DATE_STRING + "=?,"
	  			+ ATTRIBUTE_POINT_SUM_STRING + "=?"
	  			+ SQLToken.WHERE_STRING
	  			+ ATTRIBUTE_ID_STRING + "=?";
		Object[] paramsObject = new Object[]{
				object.getUser_id(),object.getTest_id(),
				object.getDate(),object.getPoint_sum(),
				object.getId()
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: delete</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#delete(java.lang.Object)
	 */
	@Override
	public boolean delete(TestLog object) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.DELETE_FROM_STRING
				+ TABLE_STRING
				+ SQLToken.WHERE_STRING
				+ ATTRIBUTE_ID_STRING + "= ?";
		Object[] paramsObject = new Object[]{
			object.getId()	
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: query</p> 
	 * <p>Description: </p> 
	 * @param id
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.TestLogDao#query(int)
	 */
	@Override
	public List<TestLog> query(int id) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_STRING
				+ SQLToken.WHERE_STRING 
				+ ATTRIBUTE_ID_STRING + "=?";
		Object[] paramsObject = new Object[]{
				id
			};
		try {
			return queryRunner.query(
					sqlString, 
					new BeanListHandler<TestLog>(TestLog.class),
					paramsObject);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	/* (non-Javadoc)
	 * <p>Title: query</p> 
	 * <p>Description: </p> 
	 * @param user
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.TestLogDao#query(cn.edu.scu.healthserver.domain.User)
	 */
	@Override
	public List<TestLog> query(UserInformation user) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_STRING
				+ SQLToken.WHERE_STRING 
				+ ATTRIBUTE_USER_ID_STRING + "=?"
				+ SQLToken.ORDER_BY_STRING
				+ ATTRIBUTE_DATE_STRING + SQLToken.DESC_STRING;
		Object[] paramsObject = new Object[]{
				user.getId()
			};
		try {
			return queryRunner.query(
					sqlString, 
					new BeanListHandler<TestLog>(TestLog.class),
					paramsObject);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	/* (non-Javadoc)
	 * <p>Title: query</p> 
	 * <p>Description: </p> 
	 * @param user
	 * @param test
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.TestLogDao#query(cn.edu.scu.healthserver.domain.User, cn.edu.scu.healthserver.domain.Test)
	 */
	@Override
	public List<TestLog> query(UserInformation user, Test test) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_STRING
				+ SQLToken.WHERE_STRING 
				+ ATTRIBUTE_USER_ID_STRING + "=?"
				+ SQLToken.AND_STRING
				+ ATTRIBUTE_TEST_ID_STRING + "=?";
		Object[] paramsObject = new Object[]{
				user.getId(),test.getId()
			};
		try {
			return queryRunner.query(
					sqlString, 
					new BeanListHandler<TestLog>(TestLog.class),
					paramsObject);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

}
